package com.ruoyi.back.service.impl

import com.baomidou.mybatisplus.extension.kotlin.KtQueryWrapper
import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl
import com.ruoyi.app.common.Data
import com.ruoyi.app.common.Fail
import com.ruoyi.app.common.Ok
import com.ruoyi.back.domain.Function
import com.ruoyi.back.mapper.FunctionMapper
import com.ruoyi.back.service.FunctionService
import com.ruoyi.common.core.page.TableSupport
import org.springframework.stereotype.Service

@Service
class FunctionServiceImpl : ServiceImpl<FunctionMapper, Function>(), FunctionService {

    override fun functionPage(): Page<Function> =
        TableSupport.buildPageRequest().let {
            page(Page(it.pageNum.toLong(), it.pageSize.toLong()), KtQueryWrapper(Function::class.java).orderByAsc(Function::orderNum))
        }

    override fun showFunction(functionId: Long): Data<Boolean> =
        getById(functionId)?.run {
            Ok("更改状态成功", updateById(this.apply {
                isShow = !isShow!!
            }))
        } ?: kotlin.run { Fail(201, "不存在当前资源") }

    override fun saveFunction(function: Function): Data<Boolean> =
        function.id?.run {
            Ok("更新功能菜单成功", updateById(function))
        } ?: Ok("保存功能菜单成功", save(function))
}